package game;

import java.awt.Point;

public class GameDice {
	
	private DiceColour dc;
	private DiceType dt;
	private Point position;
	
	public GameDice(DiceColour diceColour, DiceType diceType, Point position) {
		this.setDiceColour(diceColour);
		this.setDiceType(diceType);
		this.setPosition(position);
	}
	
	public void setDiceColour(DiceColour dc) {
		this.dc = dc;
	}

	public DiceColour getDiceColour() {
		return dc;
	}

	public void setDiceType(DiceType dt) {
		this.dt = dt;
	}

	public DiceType getDiceType() {
		return dt;
	}

	public void setPosition(Point position) {
		this.position = position;
	}

	public Point getPosition() {
		return position;
	}

	public static enum DiceColour {
		
		BLUE("b"),
		RED("r"),
		NULL("n");
		
		private final String shortName;

		private DiceColour(String shortName) {	
			this.shortName = shortName;
		}
		
		public static DiceColour parse(String shortName) {
			for (DiceColour c : values()) {
				if (c.shortName.equals(shortName))
					return c;
			}
			return null;
		}
		
		public String getShortName() {
			return shortName;
		}
	}
	
	public static enum DiceType {
		
		ONE  ("1"),
		TWO  ("2"),
		THREE("3"),
		FOUR ("4"),
		FIVE ("5"),
		SIX  ("6"),
		KEY  ("k"),
		NULL ("n");
		
		private final String shortName;
		
		private DiceType(String shortName) {	
			this.shortName = shortName;
		}
		
		public int toInt() {
			if (!shortName.equals("n")) {
				if (shortName.equals("k")) {
					return 1;
				}
				else {

					return Integer.parseInt(shortName);
				}
			}
			return 0;
		}
		
		public static DiceType parse(String ShortName) {
			for (DiceType t : values()) {
				if (t.shortName.equals(ShortName))
					return t;
			}
			return null;	
		}
		
		public String getShortName() {
			return shortName;
		}
		
	}
	
	public String toString() {
		return this.dc.toString() + "_" + this.dt.toString() + "_" + this.position.x + "_" + this.position.y;
	}
	
	public static GameDice parseFromString(String name) {
		if (name.length() != 7) throw new IllegalArgumentException("unable to parse name");
		String colour = name.substring(0, 1);
		String type   = name.substring(2, 3);
		int x	  = Integer.parseInt(name.substring(4, 5));
		int y 	  = Integer.parseInt(name.substring(6, 7));
		return new GameDice(DiceColour.parse(colour), DiceType.parse(type), new Point(x, y));
	}
	
}
